const rollup = require('rollup');

const inputOptions = {
  input: 'src/index.js',
  external: [],
  plugins: [],
};
const outputOptions = {
  dir: 'dist',
  format: 'esm',
  sourcemap: true,
  entryFileNames: '[name].[hash].js',
};

async function build() {
  let bundle;
  let bundleFailed = false;
  try {
    // 构建过程
    bundle = await rollup.rollup(inputOptions);
    // console.log("🚀 ~ bundle:", bundle)
    // console.log("🌟 ~ bundle.cache.modules:", bundle.cache.modules);
    // 输出过程
    // const resp = await bundle.generate(outputOptions); // 不输出到 磁盘
    const resp = await bundle.write(outputOptions); // 输出到 磁盘
    // console.log("👏🏻 ~ resp:", resp)
  } catch (err) {
    console.error('🚨 ~ err:', err);
    bundleFailed = true;
  }

  if (bundle) {
    await bundle.close();
  }

  // process.exit(bundleFailed ? 1 : 0)
}

build();

const watchOptions = {
  ...inputOptions,
  output: [outputOptions],
};

// src 目录下的文件发生变化时，重新构建
const watcher = rollup.watch(watchOptions);

watcher.on('event', (event) => {
  if (event.result) {
    event.result.close();
  }
  console.log('🌺 ~ event:', event);
});
